package com.mnt.washio.utils.network.ws;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;

import com.mnt.washio.objects.Card;
import com.mnt.washio.utils.Statics;
import com.mnt.washio.utils.network.NetworkUtils;
import com.mnt.washio.utils.network.WebServiceCommunicatorListener;
import com.parse.ParseException;
import com.parse.ParseUser;

@SuppressLint("SimpleDateFormat")
public class AddCreditCardWSControl extends BaseWSControl {

	private String userId;
	private String stripeId;
	private String token;

	public AddCreditCardWSControl(Context context,
			WebServiceCommunicatorListener listener, String userId,
			String stripeId, String token) {
		super(context, listener, WebServiceFlag.ADD_CARD);
		this.userId = userId;
		this.stripeId = stripeId;
		this.token = token;
	}

	@Override
	protected Object doInBackground(Void... ps) {
		// Get local database
		String url = "";
		if (stripeId != null && stripeId.length() > 0) {
			url = Statics.WASHIO_WS_URL + "payment/creditCard";
		} else {
			url = Statics.WASHIO_WS_URL + "payment/customer";
		}

		HashMap<String, String> params = new HashMap<String, String>();
		if (stripeId == null || stripeId.length() == 0) {
			params.put("userId", userId);
		} else {
			params.put("stripeId", stripeId);
		}
		params.put("tokenId", token);

		try {
			String respone = NetworkUtils.doPostRequest(url, params);

			try {
				JSONObject jsonObject = new JSONObject(respone);
				try {
					ParseUser.getCurrentUser().fetch();
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (jsonObject.has("error")) {
					if (jsonObject.get("error") instanceof String) {
						mError = jsonObject.getString("error");
						return null;
					} else {
						mError = jsonObject.getJSONObject("error")
								.getJSONObject("error").getString("message");
					}
				} else {
					if (jsonObject.has("customer")) {
						JSONObject customerJSONObject = jsonObject
								.getJSONObject("customer");
						String stripeId = customerJSONObject.getString("id");
						ParseUser.getCurrentUser().put("stripeId", stripeId);
						ParseUser.getCurrentUser().saveInBackground();
						return getCreditCards(customerJSONObject
								.getJSONObject("cards").getJSONArray("data")
								.getJSONObject(0));
					} else {
						return getCreditCards(jsonObject.getJSONObject("card"));
					}
				}
			} catch (JSONException e) {
				mError = e.getMessage();
			}

		} catch (ClientProtocolException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		} catch (IOException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		}
		return null;
	}

	public Card getCreditCards(JSONObject cardJSONObject) throws JSONException {
		Card card;
		card = new Card();
		card.setId(cardJSONObject.getString("id"));
		card.setLast4(cardJSONObject.getString("last4"));
		card.setType(cardJSONObject.getString("type"));
		card.setExpMonth(cardJSONObject.getInt("exp_month"));
		card.setExpYear(cardJSONObject.getInt("exp_year"));
		card.setFingerPrint(cardJSONObject.getString("fingerprint"));
		card.setCustomer(cardJSONObject.getString("customer"));
		card.setCountry(cardJSONObject.getString("country"));
		card.setName(cardJSONObject.getString("name"));
		card.setAddressLine1(cardJSONObject.getString("address_line1"));
		card.setAddressLine2(cardJSONObject.getString("address_line2"));
		card.setAddressCity(cardJSONObject.getString("address_city"));
		card.setAddressState(cardJSONObject.getString("address_state"));
		card.setAddressZip(cardJSONObject.getString("address_zip"));
		card.setAddressCountry(cardJSONObject.getString("address_country"));
		card.setCvcCheck(cardJSONObject.getString("cvc_check"));
		card.setAddressLine1Check(cardJSONObject
				.getString("address_line1_check"));
		card.setAddressZipCheck(cardJSONObject.getString("address_zip_check"));
		return card;
	}

}
